package com.cloudmusic.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.cloudmusic.constant.EnvConfig;
import com.cloudmusic.entity.Music;
import com.cloudmusic.utils.MD5Util;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.cloudmusic.service.UploadService;

import java.io.File;

@Controller
@RequestMapping("music/upload")
public class UploadController {

    private Logger logger=Logger.getLogger(this.getClass());

	@Autowired
    UploadService uploadService;
	
	
//	@RequestMapping("uploadandsave")
//	public String uploadAndSaveMusic(HttpServletRequest request) {
//		uploadService.uploadMusic(request);
//		return "success";
//	}

    @RequestMapping("uploadandsave")
    @ResponseBody
	public JSONObject uploadmusic(HttpServletRequest request,@RequestParam("file") MultipartFile[] files) {
        logger.info("开始上传文件~"+request.getSession().getServletContext().getRealPath(""));
	    int i = 0;
        int result = 0;
	    JSONObject json = new JSONObject();
        String location = EnvConfig.getString("MUSIC_LOCATION");
        String fileurl = location+"/musicfile";
        String jpgurl = location+"/musicjpg";
        String lrcurl = location+"/musiclrc";
        File folderfile = new File(location);
        if(!folderfile.exists()){
            logger.info("文件夹不存在，初始化文件夹");
            File filefolder = new File(fileurl);
            File jpgfolder = new File(jpgurl);
            File lrcfolder = new File(lrcurl);
            filefolder.mkdirs();
            jpgfolder.mkdirs();
            lrcfolder.mkdirs();
        }else{
            File filefolder = new File(fileurl);
            File jpgfolder = new File(jpgurl);
            File lrcfolder = new File(lrcurl);
            if(!filefolder.exists()){filefolder.mkdirs();}
            if (!jpgfolder.exists()){jpgfolder.mkdirs();}
            if (!lrcfolder.exists()){lrcfolder.mkdirs();}
        }
        try{
            if (ArrayUtils.isNotEmpty(files)){
               for (MultipartFile file : files){
                   //使用多线程上传

                   //核心音乐文件存储与解析
                   String filename = file.getOriginalFilename();
                   File musicfile = new File (fileurl+"/"+filename);
                   musicfile.createNewFile();
                   file.transferTo(musicfile);
                    //查询歌曲是否已有记录
                   if(uploadService.checkmusic(MD5Util.getFileMD5(musicfile))){
                       logger.info("库中已有"+musicfile.getName()+"歌曲信息");
                       musicfile.delete();
                       json.put("already",1);
                       throw new Exception("库中已有"+musicfile.getName()+"歌曲信息");
                   }
                   Music music = uploadService.musicanalysis(musicfile);
                   JSONObject musicinfojson = (JSONObject) JSONObject.toJSON(music);
                   logger.info(musicinfojson);
                   //返回Service数据存库
                   uploadService.uploadMusic(music);
                   i++;
               }
            }
            logger.info("成功上传"+i+"个文件~");
            result = 0;
            json.put("info",result);
        }catch (Exception e){
            logger.error("上传文件失败",e);
            result = 1;
            json.put("info",result);
        }
        return json;
    }


}
